<?php
/**
* para host username password database
* @copyright (c) 2009
* $Id: function_base.php 155 2010-05-06 05:03:17Z bwanglei $
*/

/**
* 连接数据库函数
*/
function apf_mysql_conn() {
	@ $mysql_connection = new mysqli(DB_HOST, DB_USER, DB_PASSWD, DB_NAME);
	if (mysqli_connect_errno()) {
		echo '不能连接数据库，请重试';
		exit;
	}
	mysqli_query($mysql_connection,"set names 'utf8'");
	return $mysql_connection;
}

/**
* 初始化网站参数
* @param $mysql_conn $table_name String
* @return Array
*/
function apf_get_settings($mysql_conn, $table_name) {
	$query = 'select * from `'.$table_name.'`';
	if (!$result = @$mysql_conn->query($query)) {
		apf_msg('初始化网站参数失败！');
	}
	$settings = array();
	while ($row_result = $result->fetch_assoc()) {
		$value_name = $row_result['name'];
		$settings[$value_name] = $row_result['value'];
	}
	return $settings;
}

/**
* 显示消息函数
*/
function apf_msg($message, $source=false) {
	global $apf_settings;
	if ($source===false) {
		apf_redirect(0,$apf_settings['base_url'].'/message.php?m='.$message);
	} else {
		apf_redirect(0,$apf_settings['base_url'].'/message.php?m='.$message.'&source='.$source);
	}
}

/**
* 根据数据库表中两列的数据创建一个数组，其中一列作为数组的key，另一列作为数组的value
* @param $table object 数据库查询返回对象
* @param $key_listname string 用来作为数组键名的列名称
* @param $value_listname string 用来作为数据值的列名称
*/
function apf_array_table($table, $key_listname, $value_listname) {
	$arr = array();
	while ($table_row = $table->fetch_assoc()) {
		$arr[$table_row[$key_listname]] = $table_row[$value_listname];
	}
	return $arr;
}

/**
* 页面跳转函数
*/
function apf_redirect($time, $to) {
	echo '<meta http-equiv="Refresh" content="'.$time.';URL='.$to.'" />';
}

/**
* 判断数组中是否含有某个 key 值
*/
function apf_key_in_array($key,$array) {
	$is_in = false;
	foreach ($array as $arr_key => $arr_value) {
		if ($key == $arr_key) {
			$is_in = true;
		}
	}
	return $is_in;
}

/**
* 生成上一页或者下一页的URI
*/
function apf_daohang_page_uri($canshu_name,$canshu_value,$next_pre,$get) {
	$query_string = '';
	$count = 1;
	if (apf_key_in_array($canshu_name,$get)) {
		foreach ($get as $key=>$value) {
			if ($count==1) {
				if ($key==$canshu_name and $value==$canshu_value) {
					if ($next_pre == 'next') {
						$query_string .= ('?'.$key.'='.($value+1));
					} else if ($next_pre == 'pre') {
						$query_string .= ('?'.$key.'='.($value-1));
					}
				} else {
					$query_string .= ('?'.strval($key).'='.strval($value));
				}
			} else {
				if ($key==$canshu_name and $value==$canshu_value) {
					if ($next_pre == 'next') {
						$query_string .= ('&'.$key.'='.($value+1));
					} else if ($next_pre == 'pre') {
						$query_string .= ('&'.$key.'='.($value-1));
					}
				} else {
					$query_string .= ('&'.strval($key).'='.strval($value));
				}
			}
			$count++;
		}
	} else {
		if ($next_pre == 'next') {
			$result_num_page = $canshu_value+1;
		} else if ($next_pre == 'pre') {
			$result_num_page = $canshu_value-1;
		}
		$query_string .= '?'.$_SERVER['QUERY_STRING'].'&'.$canshu_name.'='.$result_num_page;
	}
	return 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].$query_string;
}

/**
* 生成第一页和最后一页URI
function apf_first_last_uri($echo_value) {
	$count = 1;
	$query_string = '';
	$return_page_num = '';
	if (apf_key_in_array('b',$_GET)) {
		foreach ($_GET as $key => $value) {
			if ($count == 1) {
				if ($key == 'b') {
					$query_string .= ('?'.$key.'='.$echo_value);
				} else {
					$query_string .= ('?'.$key.'='.$value);
				}
			} else {
				if ($key == 'b') {
					$query_string .= ('&'.$key.'='.$echo_value);
				} else {
					$query_string .= ('&'.$key.'='.$value);
				}
			}
			$count++;
		}
	} else {
		$query_string = '?'.$_SERVER['QUERY_STRING'].'&b='.$echo_value;
	}
	return 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].$query_string;
}
*/
function apf_first_last_uri($echo_value) {
	$_GET['b'] = $echo_value;
	return 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.http_build_query($_GET);
}

/**
* 生成当前完整URL 地址 包括URL 参数，如果要取出的参数就返回去除参数后的URL地址
*/
function apf_full_url($add_name=false,$add_value=false,$del_name=false) {
	$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
	$aget = $_GET;
	if ($del_name && isset($aget[$del_name])) unset($aget[$del_name]);
	if ($add_name && ($add_value!==false) && !isset($aget[$add_name])) $aget[$add_name] = $add_value;
	$query_string = http_build_query($aget);
	if (empty($query_string)) return $url;
	return $url.'?'.$query_string;
}

/**
* 是否是管理员
*/
function apf_is_manager() {
	if (isset($_SESSION['is_admin']) && $_SESSION['is_admin']=='true') return 'root';
	if (isset($_SESSION['banzhu']) && !empty($_SESSION['banzhu'])) return unserialize($_SESSION['banzhu']);
	return false;
}
?>
